package com.atguigu.source;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.serialization.SimpleStringSchema;
import org.apache.flink.connector.kafka.source.KafkaSource;
import org.apache.flink.connector.kafka.source.enumerator.initializer.OffsetsInitializer;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class kafkaSourceDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        // todo 从Kafka读取数据：新source架构
        KafkaSource<String> kafkaSource = KafkaSource.<String>builder()
                .setBootstrapServers("hadoop102:9092")
                .setGroupId("group1")
                .setTopics("first")
                .setValueOnlyDeserializer(new SimpleStringSchema()) //指定反序列化器，只对value进行反序列化
                .setStartingOffsets(OffsetsInitializer.latest()) //指定flink消费Kafka的策略
                .build();

        env.fromSource(kafkaSource, WatermarkStrategy.noWatermarks(),"kafkaSource").print();
        env.execute();
    }
}
